﻿using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Lib.Action.Protoss
{
	public abstract class ActionProduceFromStargate : ActionProduceProtoss
	{
		protected ActionProduceFromStargate(int buildTime, int minerals, int gas, int food) : base(buildTime, minerals, gas, food) { }

		public override int AvailableProducer(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return state.Stargate.AvailableForProduction;
		}

		public abstract string EventName { get; }
		public abstract void OnProductionFinished(IBuildOrder bo);

		public override bool HaveTechnology(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return state.Stargate.Count > 0;
		}

		public override bool IsInvalid(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return state.Stargate.Total == 0 || base.IsInvalid(bo);
		}

		public override void Execute(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			base.Execute(bo);
			state.Stargate.Production++;
			bo.AddAction(state.Time + BuildTime, new ActionGeneric(EventName, (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
			{
				ProtossState ibostate = ibo.State as ProtossState;
				ibostate.Stargate.Production--;
				OnProductionFinished(ibo);
			}));
		}

		protected IAction[,] m_Requirements;
		public override IAction[,] Requirements
		{
			get
			{
				if (m_Requirements == null)
					m_Requirements = new IAction[,] { { ActionProduceStargate.Instance } };
				return m_Requirements;
			}
		}
	}

	public class ActionProducePhoenix : ActionProduceFromStargate
	{
		public static ActionProducePhoenix Instance = new ActionProducePhoenix();
		private ActionProducePhoenix() : base(45, 150, 100, 2) { }
		public override string EventName { get { return "ActionSpawnPhoenix"; } }
		public override void OnProductionFinished(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			state.Phoenix++;
		}
	}
	public class ActionProduceVoidRay : ActionProduceFromStargate
	{
		public static ActionProduceVoidRay Instance = new ActionProduceVoidRay();
		private ActionProduceVoidRay() : base(60, 250, 150, 3) { }
		public override string EventName { get { return "ActionSpawnVoidRay"; } }
		public override void OnProductionFinished(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			state.VoidRay++;
		}
	}
	public class ActionProduceCarrier : ActionProduceFromStargate
	{
		public static ActionProduceCarrier Instance = new ActionProduceCarrier();
		private ActionProduceCarrier() : base(120, 350, 250, 6) { }
		public override string EventName { get { return "ActionSpawnCarrier"; } }
		public override void OnProductionFinished(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			state.Carrier++;
		}
		public override bool HaveTechnology(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return base.HaveTechnology(bo) && state.FleetBeacon.Count > 0;
		}
		public override bool IsInvalid(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return base.IsInvalid(bo) || state.FleetBeacon.Total == 0;
		}
		public override IAction[,] Requirements
		{
			get
			{
				if (m_Requirements == null)
					m_Requirements = new IAction[,] { { ActionProduceFleetBeacon.Instance } };
				return base.Requirements;
			}
		}
	}
}
